import { createRouter, createWebHistory } from "vue-router";
// 引入NProgress
import NProgress from "nprogress";
// 引入nprogress样式文件
import "nprogress/nprogress.css";

import { useUserStore } from "@/stores/user.js";
import { useRoutesStore } from "@/stores/routes.js";
// import pinia from '@/stores/index.js'
// import { useUserStore } from "@/stores/user.js";
// const userStore = useUserStore(pinia)

//createWebHistory   不带#
//createWebHashHistory 带有#
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL), //提供一个环境变量，，用来区分开发环境和生产环境
  routes: [
    {
      path: "/login",
      component: () => import("@/views/login/loginPage.vue"),
    },
    {
      path: "/",
      component: () => import("@/views/layout/layoutContainer.vue"),
      redirect: "/article/channel",
      children: [
        {
          path: "/article/channel",
          component: () => import("@/views/article/articleChannel.vue"),
        },
        {
          path: "/article/manage",
          component: () => import("@/views/manager/articleManager.vue"),
        },
        {
          path: "/personalCenter/basicInformation",
          component: () =>
            import("@/views/personalCenter/basicInformation.vue"),
        },
        {
          path: "/personalCenter/resetAvator",
          component: () => import("@/views/personalCenter/resetAvator.vue"),
        },
        {
          path: "/personalCenter/resetPassword",
          component: () => import("@/views/personalCenter/resetPassword.vue"),
        },
      ],
    },
    { path: "/404", name: "404", component: () => import("@/views/404.vue") },
    {
      path: "/:pathMatch(.*)",
      redirect: "/404",
    },
  ],
  // 滚动行为:控制滚动条的位置
  scrollBehavior() {
    return {
      left: 0,
      top: 0,
    };
  },
});
// 白名单
const whiteList = ["/login"];
// 全局前置导航守卫  鉴权
// 路由导航前，默认直接放行
//返回值
// 1.underfined  /true
// 2.false 拦回from地址
// 3.具体路径  拦截后，跳转到指定地址
//'/login',  {name: 'login'}
router.beforeEach((to, from) => {
  NProgress.start();
  // 是否有token
  const userStore = useUserStore();
  const routes = useRoutesStore();
  console.log(routes, "routes");
  const token = userStore.token;
  // const token = getToken()
  // 登录过，去的地址是否是登录页
  // if (token) {
  //   if (to.path === '/login') {
  //     next('/')
  //   } else {
  //     // 不是登录页，是否有用户资料
  //     // const nav = store.getters.nav

  //     // if (nav) {
  //     //     // 有用户资料,放行
  //     //     next()
  //     // } else {
  //     //     // 没有就获取一下，再放行

  //     //     next()
  //     // }
  //     next()
  //   }
  // } else {

  //   // 没有登陆过
  //   if (whiteList.includes(to.path)) {
  //     next()
  //   } else {
  //     next('/login')
  //   }
  // }
  console.log(token, "token");
  console.log(to.path, "to.path");
  if (token && to.path == "/login") return from.path;
  if (!token && to.path !== "/login") return "/login";
  // 关闭进度条
  NProgress.done();
});
// // 后置守卫
router.afterEach((to, from) => {
  // 关闭进度条
  NProgress.done();
});
export default router;
